<?php
/**
 * Created by PhpStorm.
 * User: kai.qiu
 * Date: 17/3/20
 * Time: 下午9:32
 */

namespace App\Libs;

trait DataFormat
{

    public static function format($data)
    {
        if($data == null)
        {
            $data = (object)[];
        }
        if(is_object($data))
        {
            $data =  $data instanceof Self?  (array) $data->attributes : (array) $data;
        }
        $data = isset(self::$formatKey) ? array_only($data, array_keys(self::$formatKey)):[];

        foreach($data as $key=>$value)
        {
            switch(self::$formatKey[$key])
            {
                case 'int':
                    $data[$key] = (int) $value;
                    break;

                case 'double':
                    $data[$key] = (double) $value;
                    break;

                case 'string':
                    $data[$key] = (string) $value;
                    break;

                case 'url':
                    //$data[$key] = oss_url($value);
                    break;

                case 'image_url':
                    $data[$key] = image_url($value);
                    break;

                case 'default':
                    //$data[$key] = oss_url($value);
                    break;

                case 'json_decode':
                    $data[$key] = json_decode($value, true);
                    break;

                default:;
            }
        }

        return $data;
    }
}